package indi.gao.leetcode.problem;
//反转一个单链表。
//
// 示例:
//
// 输入: 1->2->3->4->5->NULL
//输出: 5->4->3->2->1->NULL
//
// 进阶:
//你可以迭代或递归地反转链表。你能否用两种方法解决这道题？
// Related Topics 链表


//leetcode submit region begin(Prohibit modification and deletion)

import java.util.HashMap;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode(int x) { val = x; }
 * }
 */
public class ReverseLinkList {
    public static void main(String[] args) {
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(3);
        ListNode listNode4 = new ListNode(4);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = null;
        ListNode listNode = revertList(listNode1);
        sop(listNode);
    }

    public static ListNode revertList(ListNode listNode) {
        ListNode head = listNode;
        ListNode olderHead = head;
        ListNode point;
        if (head != null) {
            point = head.next;
        } else {
            point = null;
        }
        ListNode pointNext;
        while (point != null) {
            pointNext = point.next;
            olderHead.next = olderHead.next.next;
            point.next = head;
            head = point;
            point = pointNext;
        }
        return head;
    }

    public static void sop(ListNode listNode) {
        while (listNode != null) {
            System.out.println(listNode.val);
            listNode = listNode.next;
        }
    }
}

class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }
}
